package cn.iocoder.yudao.module.custom.controller.admin.city;

import cn.iocoder.yudao.module.custom.convert.province.ProvinceConvert;
import cn.iocoder.yudao.module.custom.dal.dataobject.province.ProvinceDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.custom.controller.admin.city.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.city.CityDO;
import cn.iocoder.yudao.module.custom.convert.city.CityConvert;
import cn.iocoder.yudao.module.custom.service.city.CityService;

/**
 * @author Administrator
 */
@Tag(name = "管理后台 - 城市信息")
@RestController
@RequestMapping("/custom/city")
@Validated
public class CityController {

    @Resource
    private CityService cityService;

    @PostMapping("/create")
    @Operation(summary = "创建城市信息")
    @PreAuthorize("@ss.hasPermission('custom:city:create')")
    public CommonResult<String> createCity(@Valid @RequestBody CityCreateReqVO createReqVO) {
        return success(cityService.createCity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新城市信息")
    @PreAuthorize("@ss.hasPermission('custom:city:update')")
    public CommonResult<Boolean> updateCity(@Valid @RequestBody CityUpdateReqVO updateReqVO) {
        cityService.updateCity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除城市信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('custom:city:delete')")
    public CommonResult<Boolean> deleteCity(@RequestParam("id") String id) {
        cityService.deleteCity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得城市信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('custom:city:query')")
    public CommonResult<CityRespVO> getCity(@RequestParam("id") String id) {
        CityDO city = cityService.getCity(id);
        return success(CityConvert.INSTANCE.convert(city));
    }

    @GetMapping("/list")
    @Operation(summary = "获得城市信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('custom:city:query')")
    public CommonResult<List<CityRespVO>> getCityList(@RequestParam("ids") Collection<String> ids) {
        List<CityDO> list = cityService.getCityList(ids);
        return success(CityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得城市信息分页")
    @PreAuthorize("@ss.hasPermission('custom:city:query')")
    public CommonResult<PageResult<CityRespVO>> getCityPage(@Valid CityPageReqVO pageVO) {
        PageResult<CityDO> pageResult = cityService.getCityPage(pageVO);
        return success(CityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出城市信息 Excel")
    @PreAuthorize("@ss.hasPermission('custom:city:export')")
    @OperateLog(type = EXPORT)
    public void exportCityExcel(@Valid CityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CityDO> list = cityService.getCityList(exportReqVO);
        // 导出 Excel
        List<CityExcelVO> datas = CityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "城市信息.xls", "数据", CityExcelVO.class, datas);
    }

    @GetMapping("/getCityLists/{parentId}")
    @Operation(summary = "获得商户信息分页")
    public CommonResult getCityLists(@PathVariable Integer parentId) {
        List<CityDO> cityLists = cityService.getCityLists(parentId);
        return success(CityConvert.INSTANCE.convertList(cityLists));
    }

}
